<?php defined('BASEPATH') or exit('No direct script access allowed');

class Myshows_m extends MY_Model {
	var $task= "";
	function __construct(){
		parent::__construct();
		$this->load->library('module_helper');
		
	}
	
	function all_shows(){
		$total = count( $this->db->where('user_created_id',$_SESSION['user']['id'])->get(TBL_SHOW)->result() );
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : defaultNumberRecordsPerPageShowDataConfig();
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'seller/my_shows/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->where('user_created_id',$_SESSION['user']['id'])->order_by('id','desc')->limit($records_per_page, $per_page )->get(TBL_SHOW)->result();
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function past_shows(){
		$where = "status IN('closed', 'cancelled') AND user_created_id=".$_SESSION['user']['id']."";
		$total = count( $this->db->where( $where )->get(TBL_SHOW)->result() );
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : defaultNumberRecordsPerPageShowDataConfig();
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'seller/my_shows/past_shows/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->where($where)->order_by('id','desc')->limit($records_per_page, $per_page )->get(TBL_SHOW)->result();
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function upcoming_shows(){
		$where = "status LIKE 'opening' AND user_created_id=".$_SESSION['user']['id']."";
		$total = count( $this->db->where($where)->get(TBL_SHOW)->result() ); 
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : defaultNumberRecordsPerPageShowDataConfig();
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){
			if($key != "per_page")
				$partial["$key"] = "$key=$value";
		}
		
		$pagination = create_pagination( 
								$uri = 'seller/my_shows/upcoming_shows/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->where($where)->order_by('id','desc')->limit($records_per_page, $per_page )->get(TBL_SHOW)->result();
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	function search(){
		$condition = array();
		if($_GET['title'] != "Title"){
			$title = mysql_real_escape_string(strtolower($_GET['title']));
			$condition[] = " LOWER(name) LIKE '$title%' ";
		}
		if($_GET['country_origin'] != ""){
			$country_origin = mysql_real_escape_string(strtolower($_GET['country_origin']));
			$condition[] = " country_origin_code LIKE '$country_origin' ";
		}
		if($_GET['artist'] != "Artist"){
			$artist = mysql_real_escape_string(strtolower($_GET['artist']));
			$condition[] = " artist LIKE '$artist%' ";
		}
		if($_GET['bid'] != "Minimum bid"){
			$bid = intval($_GET['bid']);
			$condition[] = " min_bid >= $bid ";
		}
		if($_GET['show_type'] != "" ){
			$show_type = mysql_real_escape_string($_GET['show_type']);
			$condition[] = " show_type LIKE '$show_type' ";
		}
		if($_GET['start_date'] != "From Date"){
			$start_date = convertToTimeStampFromFormat('j-M-Y',$_GET['start_date']);
			if($start_date != 0){
				$condition[] = " start_date >= $start_date ";
			}
		}
		if($_GET['end_date'] != "To Date"){
			$end_date = convertToTimeStampFromFormat('j-M-Y',$_GET['end_date']);
			if($end_date != 0){
				$condition[] = " end_date <= $end_date ";
			}
		}
		if($_GET['show_status'] != ""){
			$show_status = mysql_real_escape_string($_GET['show_status']);
			$condition[] = " status LIKE '$show_status' ";
		}
		$condition_str = implode('AND', $condition);
	    $condition_str = !empty($condition_str) ? $condition_str : '1';
		
		$total = count( $this->db->query("SELECT * FROM ".TBL_SHOW." WHERE $condition_str AND user_created_id=".$_SESSION['user']['id']."")->result() ); 
		$records_per_page = isset($_GET['records_per_page']) ? intval($_GET['records_per_page']) : defaultNumberRecordsPerPageShowDataConfig();
		$per_page = isset($_GET['per_page']) ? intval($_GET['per_page']) : 0;
		
		$partial = array();
		foreach($_GET as $key=> $value){  
			if($key != "per_page")
				$partial["$key"] = "$key=".urlencode($value);
		}
	 
		$pagination = create_pagination( 
								$uri = 'seller/search/?'.implode('&',$partial), 
								$total_rows = $total , 
								$limit= $records_per_page,
								$uri_segment = 0,
								TRUE, TRUE 
							);
	 	 
		$records = $this->db->query("SELECT * FROM ".TBL_SHOW." WHERE $condition_str AND user_created_id=".$_SESSION['user']['id']." ORDER BY id DESC LIMIT $per_page, $records_per_page")->result();
		return array('pagination'=> $pagination, 'items'=>$records, 'total_records'=>$total);
	}
	
	
	
}